<template>
  <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px" class="login-container">
    <h3 class="login-title">登录系统</h3>
    <el-form-item label="用户名" label-width="80px" prop="username" class="username">
      <el-input type="input" v-model="form.username" auto-complete="off" placeholder="请输入账户"></el-input>
    </el-form-item>
    <el-form-item label="密码" label-width="80px" prop="password">
      <el-input type="password" v-model="form.password" auto-complete="off" placeholder="请输入密码"></el-input>
    </el-form-item>
    <template class="login_radio">
      <el-radio v-model="radio" label="1">客户</el-radio>
      <el-radio v-model="radio" label="2">商户</el-radio>
    </template>
    <el-form-item class="login_submit">
      <el-button type="primary" @click="submitForm('form')">提交</el-button>
      <el-button @click="resetForm('form')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import axios from "axios"
// import {userRouter,userRoute} from "vue-router";
//
// const Router=userRouter();
// const Route=userRoute()
export default {
  name: "LoginView",
  data() {
    return {
      radio: '1',
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, message: '用户名长度不小于2位', trigger: 'blur'}
        ],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      }
    }
  }, methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const me = this;
          if (this.radio == 1) {
            axios.get('http://localhost:3000/a', {
              params: {
                username: me.form.username,
                password: me.form.password
              }
            }).then((res) => {
              console.log(res.data.data[0])
              this.$store.commit('setPetowner',res.data.data[0]);
              me.$router.push('/o')
            })
          } else {
            axios.get('http://localhost:3000/b', {
              params: {
                username: me.form.username,
                password: me.form.password
              }
            }).then((res) => {
              console.log(res.data.data[0])
              this.$store.commit('setPetstore',res.data.data[0]);
              me.$router.push('/s')
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>

<style>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: auto;
  width: 350px;
  padding: 35px 35px 35px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login-title {
  margin: 0 auto 40px auto;
  text-align: center;
  color: #333333;
}

.login_submit {
  margin: 10px auto 0px auto;
}

.login_radio {
  margin: 0 auto;
}
</style>